
let app = getApp();

const config = require('../../common/config');
const util = require('../../utils/util');
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
var locationStrLen = 7; //定位地点文字显示长度
var userLongitude, userLatitude;
var placeAddress=[];

Page({
    data: {
        loadStatus:false, //加载状态
        hasDiscoveryListData:false,
        currentCity : '常州市',
        currentDistrict : '溧阳市',
        region: [],
        customItem: '',
        special : []
    },
      onLoad:function () {
        var that = this;
        // 实例化API核心类
        qqmapsdk = new QQMapWX({
            key: config.QQMAPSDK_KEY
        });
        //定位
        userLatitude = wx.getStorageSync(config.LOC_LATITUDE);
        userLongitude = wx.getStorageSync(config.LOC_LONGITUDE);

        if(userLatitude!=null && userLatitude!='' && userLongitude!=null && userLongitude!=''){
            // 根据坐标调用接口获取地理名称
            qqmapsdk.reverseGeocoder({
                location: {
                    latitude: userLatitude,
                    longitude: userLongitude
                },
                success: function(res) {
                    var tempCurrentAddress = [res.result.address_component.province,res.result.address_component.city,res.result.address_component.district];
                    var tempCurrentCity = tempCurrentAddress[1],
                        tempCurrentDistrict = tempCurrentAddress[2];

                    that.setData({
                        currentCity : tempCurrentCity.length>locationStrLen?tempCurrentCity.substring(0,locationStrLen)+'.':tempCurrentCity,
                        currentDistrict : tempCurrentDistrict.length>locationStrLen?tempCurrentDistrict.substring(0,locationStrLen)+'.':tempCurrentDistrict,
                        region:tempCurrentAddress
                    });
                    placeAddress=tempCurrentAddress;
                    getDiscoveryList(that,'place');

                },
                fail: function(res) {
                    util.debug("discovery/indexjs:获取定位地名信息失败:");
                    util.debug(res);
                    getDiscoveryList(that,'coordinate');
                }
            });
        }else{
            wx.getLocation({
                type: 'wgs84',
                success: function(res) {
                    userLatitude = res.latitude
                    userLongitude = res.longitude
                    var speed = res.speed
                    var accuracy = res.accuracy

                    wx.setStorageSync(config.LOC_LATITUDE,userLatitude);
                    wx.setStorageSync(config.LOC_LONGITUDE,userLongitude);

                    // 调用接口
                    qqmapsdk.reverseGeocoder({
                        location: {
                            latitude: userLatitude,
                            longitude: userLongitude
                        },
                        success: function(res) {
                            var tempCurrentAddress = [res.result.address_component.province,res.result.address_component.city,res.result.address_component.district];
                            var tempCurrentCity = tempCurrentAddress[1],
                                tempCurrentDistrict = tempCurrentAddress[2];

                            that.setData({
                                currentCity : tempCurrentCity.length>locationStrLen?tempCurrentCity.substring(0,locationStrLen)+'.':tempCurrentCity,
                                currentDistrict : tempCurrentDistrict.length>locationStrLen?tempCurrentDistrict.substring(0,locationStrLen)+'.':tempCurrentDistrict,
                                region:tempCurrentAddress
                            });
                            placeAddress=tempCurrentAddress;
                            getDiscoveryList(that,'place');

                        },
                        fail: function(res) {
                            util.debug("discovery/indexjs:获取定位地名信息失败:");
                            util.debug(res);
                            getDiscoveryList(that,'coordinate');
                        },
                        complete: function(res) {

                        }
                    });

                },
                fail: function(res) {
                    getDiscoveryList(that,'none');
                    util.debug("discovery/indexjs:获取经纬度失败:");
                    util.debug(res);
                }
            });
        }
    },
    onReady:function () {

    },
    toSearchPage: function (e) {
            wx.navigateTo({
                url: '../../pages/search/index'
            })
    },
    refreshPage:function (e) {
        util.debug(e)
    },
    nextPage:function (e) {
        util.debug(e)
    },
    bindRegionChange: function (e) {
        var tempProvice = e.detail.value[0],
            tempCurrentCity = e.detail.value[1],
            tempCurrentDistrict = e.detail.value[2],
            tempSelect = tempCurrentDistrict;

        if(tempSelect == '全部'){
            tempSelect = tempCurrentCity;
        }

        if(tempSelect == '全部'){
            tempSelect = tempProvice;
        }
        this.setData({
            region: e.detail.value,
            currentDistrict:tempSelect.length>locationStrLen?tempSelect.substring(0,locationStrLen)+'.':tempSelect
        })
        placeAddress=e.detail.value;
        getDiscoveryList(this,'place');
    },
})

//根据所选或定位城市获取发现列表
function getDiscoveryList(thisPage,type) {
    thisPage.setData({
        loadStatus: true,
        special:[]
    })
    var sortType;
    if(type == 'none'){
        sortType = 2;
        userLongitude = 0;
        userLatitude = 0;
        placeAddress = [0,0,0];
        util.debug("discovery/indexjs:获取发现列表,无地点信息，采用默认地点");
    } else if(type == 'coordinate'){
        sortType = 2;
        placeAddress = [0,0,0];
        util.debug("discovery/indexjs:获取发现列表，采用定位经纬坐标信息");
        util.debug(userLongitude+","+userLatitude);
    } else if(type == 'place'){
        sortType = 3;
        util.debug("discovery/indexjs:获取发现列表，采用定位地名信息");
        util.debug(placeAddress);
    }

    wx.request({
        url: config.COMMPT_REQPATH,
        header: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        method:'POST',
        data: {
            accessToken: '',
            refreshToken: '',
            serviceName: 'wxShopManageService',
            methodName: 'getShopList',
            reqData:JSON.stringify({
                sortType: sortType,
                latitute: userLatitude,
                longitude: userLongitude,
                province:placeAddress[0],
                city:placeAddress[1],
                district:placeAddress[2]
            })
        },
        success: function (res) {
            var resultData = res.data;

            thisPage.setData({
                loadStatus: false
            })
            if(resultData.code == 1){
                var bzShopList = resultData.data.bzShopList;
                if(bzShopList.length>0){
                    thisPage.setData({
                        special: bzShopList,
                        hasDiscoveryListData: true
                    })
                }else {
                    thisPage.setData({
                        special: [],
                        hasDiscoveryListData: false
                    })
                }
            }
        },
        fail:function (errResult) {
            thisPage.setData({
                special: [],
                hasDiscoveryListData: false
            })
            util.debug(errResult);
        },
        complete:function () {
            thisPage.setData({
                loadStatus: false
            })
        }
    })
}